package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.messageBean.MessageListBean;
import com.oscar.ehome.bean.pagebean.HuokeBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.xrefresh.XRefreshView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.HuokeListAdapter;
import com.oscar.ehome.view.pageview.view.HuokeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/10/12
 * 霍克王列表
 */

public class HuokeListActivity extends BasesActivity<HuokeView, PagePresenter> implements AdapterView.OnItemClickListener, XRefreshView.XRefreshViewListener, HuokeView {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * list view
     */
    @BindView(R.id.huoke_list)
    ListView huokeList;
    /**
     *
     */
    @BindView(R.id.huoke_xrefresh)
    XRefreshView huokeXrefresh;

    String token = "";
    public static long lastRefreshTime;
    PagePresenter pagePresenter;
    private int page = 1;//页数
    private int page_size = 20;//每页的条数
    HuokeListAdapter adapter;
    private List<HuokeBean.HuokeListBean> lists = new ArrayList<HuokeBean.HuokeListBean>();//数据源


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huokelist);
        ButterKnife.bind(this);
        initView();
        onClicks();
        initData();
    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }

    /**
     *
     */
    public void initView() {

        titleImgBack.setVisibility(View.VISIBLE);
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleTopbar.setText(getResources().getString(R.string.huokehd));

        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(HuokeListActivity.this).getSystemConfig().getColor()));
        token = SystemConfigFactory.getInstance(HuokeListActivity.this).getSystemConfig().getToken();

        adapter = new HuokeListAdapter(HuokeListActivity.this, lists);
        huokeList.setAdapter(adapter);

    }

    /**
     * 请求数据
     */
    public void initData() {
        String s = String.valueOf(page);
        pagePresenter.getHuokeList(token, s, "20");
        showLoading();
    }

    /**
     * 点击事件
     */
    public void onClicks() {
        huokeList.setOnItemClickListener(this);

        huokeXrefresh.setXRefreshViewListener(this);
        // 设置是否可以下拉刷新
        huokeXrefresh.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        huokeXrefresh.setPullLoadEnable(true);
        // 设置上次刷新的时间
        huokeXrefresh.restoreLastRefreshTime(lastRefreshTime);
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        huokeXrefresh.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        huokeXrefresh.setAutoRefresh(false);

    }


    @OnClick({R.id.title_layout_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left://返回
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String addapp = "";
        if (lists.size() > 0) {
            if (lists.get(position) != null) {
                Log.e("huokkefanhui", "onItemClick: "+ lists.get(position));
                addapp = new Gson().toJson(lists.get(position));
            }
        }
        Intent intent = new Intent(HuokeListActivity.this, HuokeDetailActivity.class);
        intent.putExtra("context", addapp);
        startActivity(intent);
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onRefresh(boolean isPullDown) {
        lastRefreshTime = huokeXrefresh.getLastRefreshTime();
        huokeXrefresh.restoreLastRefreshTime(lastRefreshTime);
        page = 1;
        initData();
    }

    @Override
    public void onLoadMore(boolean isSilence) {
        page++;
        initData();
//        huokeXrefresh.stopLoadMore();
    }

    @Override
    public void onRelease(float direction) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY) {

    }

    @Override
    public void getHuokeList(HuokeBean result) {
        dismissLoading();
        huokeXrefresh.stopRefresh();
        if (result != null) {
            if (page == 1) {
                lists.clear(); // 将原有的内容清空
            }
            lists = result.getList();
            if (lists.size() > 0) {
                adapter.setLists(lists);
            }
            if (lists.size() >= 20) {
                huokeXrefresh.setPullLoadEnable(true);
            } else {
                huokeXrefresh.setPullLoadEnable(false);
            }
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(HuokeListActivity.this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(HuokeListActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(HuokeListActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(HuokeListActivity.this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(HuokeListActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
